const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiving_address: [],
    checkSelect:true,
    mobile_phone:'',
    showCheck:false
  },
  //查询收货地址列表
	getUserInfo(mobile_phone) {
		let data = {
			token: wx.getStorageSync('token'),
			mobile_phone: mobile_phone
		}
		let _this = this
		app.request('get', '/user/getUserInfo', data, res => {
      let userInfo = res.data.data
      console.log(userInfo);
			_this.setData({
        'receiving_address': userInfo.receiving_address,
        showCheck:true
      })
      
		})
  },
// 修改默认
  checkboxChange(e){
    this.setData({
      showCheck:false
    })
    let id=e.target.dataset.addressid;
    let oldArr=this.data.receiving_address

    let obj=this.data.receiving_address.filter(res=>{
      return res.id==id
    })
    obj[0].is_default=!(obj[0].is_default)
    let mobile_phone=this.data.mobile_phone
    let data = {
     
      receiving_address:obj[0]
		}
		let _this = this
		app.request('post', `/user/editUserInfo?mobile_phone=${mobile_phone}`, data, res => {
      console.log(res);
      let userInfo = res.data.data
      
			_this.setData({
        'receiving_address': userInfo.receiving_address,
        showCheck:true
      })
      console.log(_this.data.receiving_address);
		})
  },
  //编辑地址
  editAddress(e){
    let id=e.target.dataset.addressid;
    let mobile_phone=this.data.mobile_phone
    
    wx.navigateTo({
      url: `/pages/editAddress/editAddress?id=${id}&mobile_phone=${mobile_phone}`,
    })
  },
//删除地址
  deleteAddress(e){
    let id=e.target.dataset.addressid;
    let mobile_phone=this.data.mobile_phone
    let data = {
      receiving_address: {id:id}
    }
    let _this = this
    app.request('post', `/user/editUserInfo?mobile_phone=${mobile_phone}`, data, res => {

      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 500
      })
      _this.getUserInfo(mobile_phone)
    })
  },


  //新增地址
  toAddAddress(e){
    console.log(1111);
    let id=''
    let mobile_phone=this.data.mobile_phone
    
    wx.navigateTo({
      url: `/pages/editAddress/editAddress?id=${id}&mobile_phone=${mobile_phone}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      mobile_phone:options.mobile_phone
    })
    this.getUserInfo(options.mobile_phone)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})